package com.shm.leetcode.concurrent;

import java.util.concurrent.Semaphore;
import java.util.function.IntConsumer;
/**
 * @author: shm
 * @dateTime: 2020/10/18 19:17
 * @description: 解题思路
 * 通过三个信号量来控制
 * zero方法中的for表示要输出的0个次数，同时用来控制要唤醒偶数还是奇数方法
 * even方法用来输出偶数同时唤醒zero方法
 * odd方法用来输出奇数同时唤醒zero方法
 *
 * 作者：dan-xie-6
 * 链接：https://leetcode-cn.com/problems/print-zero-even-odd/solution/xin-hao-liang-jie-jue-by-dan-xie-6/
 */
public class ZeroEvenOdd1 {
    private int n;

    public ZeroEvenOdd1(int n) {
        this.n = n;
    }

    private Semaphore zero = new Semaphore(1);
    private Semaphore odd = new Semaphore(0);
    private Semaphore even = new Semaphore(0);

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            zero.acquire();
            printNumber.accept(0);
            if (i%2!=0){
                odd.release();
            }else {
                even.release();
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        for (int i = 2; i <= n; i+=2) {
            even.acquire();
            printNumber.accept(i);
            zero.release();
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i+=2) {
            odd.acquire();
            printNumber.accept(i);
            zero.release();
        }
    }

    public static void main(String[] args) {
        ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(10);
        new Thread(() -> {
            try {
                zeroEvenOdd.zero(System.out::print);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                zeroEvenOdd.even(System.out::print);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                zeroEvenOdd.odd(System.out::print);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

//    作者：dan-xie-6
//    链接：https://leetcode-cn.com/problems/print-zero-even-odd/solution/xin-hao-liang-jie-jue-by-dan-xie-6/
}
